package com.eastcom.providor.task;

import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.eastcom.bm.atom.Atom_DatasetDefine;
import com.eastcom.bm.atom.DataSetStatiBean;
import com.eastcom.providor.dao.impl.DataSetDao;

public class DataSetMonthStatisRoutine {
	
	private DataSetDao dataSetDao;
	
	public void setDataSetDao(DataSetDao dataSetDao) {
		this.dataSetDao = dataSetDao;
	}

	public DataSetDao getDataSetDao() {
		return dataSetDao;
	}
	
	public void doRoutine(){
		//Date startDate=getStartTimeOfMonthApart(-1);
		//Date endDate=getStartTimeOfMonthApart(0);
		
		Date startDate=getStartTimeOfDayApart(new Date(),-30);
		Date endDate=getStartTimeOfDayApart(new Date(),0);
		
		System.out.println(new Date()+" DataSetMonthStatisRoutine doRoutine with startDate "+startDate+" endDate "+endDate);
		
		try {
			List<Atom_DatasetDefine> datasetDefines=dataSetDao.selectAllDatasetDefine();
			//List<Atom_DatasetDefine> datasetDefines=dataSetDao.selectDatasetDefineByNeTypeAndCName(null,null ,false);
			int size=datasetDefines==null?0:datasetDefines.size();
			System.out.println(new Date()+" datasetDefines "+size);
			if(datasetDefines==null||datasetDefines.isEmpty())
				return;
			
			for(Atom_DatasetDefine datasetDefine:datasetDefines){
				System.out.println(new Date()+" DataSetMonthStatisRoutine doRoutine with dataSet "+datasetDefine.getId());
				
				Date tempStart=startDate;
				List<DataSetStatiBean> saveBeans=new LinkedList<DataSetStatiBean>();
				
				for(int i=0;i<4;i++){
					Date tempEnd=i==3?endDate:getStartTimeOfDayApart(tempStart,8);
					List<DataSetStatiBean> dataSetStatiBeans=dataSetDao.selectDataSetStatiBeanWithDayGranu(datasetDefine.getId(), tempStart, tempEnd);
					if(dataSetStatiBeans==null||dataSetStatiBeans.isEmpty()){
						tempStart=tempEnd;
						continue;
					}
					
					//dataSetDao.removeDataSetStatisByParam(datasetDefine.getId(), tempStart, tempEnd);
					//dataSetDao.saveDataSetStatis(dataSetStatiBeans);
					saveBeans.addAll(dataSetStatiBeans);
					tempStart=tempEnd;
				}
				
				dataSetDao.removeDataSetStatisByParam(datasetDefine.getId(), startDate, endDate);
				System.out.println(new Date()+" DataSetMonthStatisRoutine begin to save "+datasetDefine.getId()+" with size "+saveBeans.size());
				dataSetDao.saveDataSetStatisByJdbc(saveBeans);
				System.out.println(new Date()+" DataSetMonthStatisRoutine saved "+datasetDefine.getId()+" with size "+saveBeans.size());
				System.out.println(new Date()+" DataSetMonthStatisRoutine doRoutine "+datasetDefine.getId()+" completed");
			}
			
			System.out.println(new Date()+" DataSetMonthStatisRoutine doRoutine completed");
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	private Date getStartTimeOfMonthApart(int monthApart){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(new Date());
		
		calendar.add(Calendar.MONTH, monthApart);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		
		return calendar.getTime();
	}
	
	private Date getStartTimeOfDayApart(Date date,int dayApart){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		
		calendar.add(Calendar.DAY_OF_YEAR, dayApart);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		
		return calendar.getTime();
	}
	
	private int getDayCountOfMonth(Date date){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		
		calendar.add(Calendar.MONTH, 1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		
		return calendar.get(Calendar.DAY_OF_MONTH);
	}
	
	public static void main(String[] args){
		try {
			FileSystemXmlApplicationContext context=new FileSystemXmlApplicationContext("../config/routineContext.xml");

		} 
		catch (Exception e) {
			e.printStackTrace();
		}
		
	}
}
